/*
    mug18n - Translating your SmugMug gallery to any language.
    Copyright (c) 2008 Lukasz Kowalczyk

    Get the latest version at http://mug18n.googlecode.com

    If you have any comments or questions, contact me at the Digital Grin Forums
    (http://dgrin.com, my username there is "fliegermaus").

    $Id: header-javascript.js 28 2008-11-21 15:46:38Z lkowalczyk $
*/

/*global YE, YD, sm_i18n */
//var YE = YE || {};
//var YD = YD || {};

sm_i18n.photoSizes = function (html) {
    if (!html) {
        return html;
    }
    if (sm_i18n.messages.PhotoSizeS) {
        html = html.replace(/>S</g, '>'+sm_i18n.messages.PhotoSizeS+'<');
    }
    if (sm_i18n.messages.PhotoSizeM) {
        html = html.replace(/>M</g, '>'+sm_i18n.messages.PhotoSizeM+'<');
    }
    if (sm_i18n.messages.PhotoSizeL) {
        html = html.replace(/>L</g, '>'+sm_i18n.messages.PhotoSizeL+'<');
    }
    if (sm_i18n.messages.PhotoSizeXL) {
        html = html.replace(/>XL</g, '>'+sm_i18n.messages.PhotoSizeXL+'<');
    }
    if (sm_i18n.messages.PhotoSizeX2L) {
        html = html.replace(/>X2</g, '>'+sm_i18n.messages.PhotoSizeX2L+'<');
    }
    if (sm_i18n.messages.PhotoSizeO) {
        html = html.replace(/>O</g, '>'+sm_i18n.messages.PhotoSizeO+'<');
    }
    if (sm_i18n.messages.PhotoSizeSmall) {
        html = html.replace(/>Small</g, '>'+sm_i18n.messages.PhotoSizeSmall+'<');
    }
    if (sm_i18n.messages.PhotoSizeMedium) {
        html = html.replace(/>Medium</g, '>'+sm_i18n.messages.PhotoSizeMedium+'<');
    }
    if (sm_i18n.messages.PhotoSizeLarge) {
        html = html.replace(/>Large</g, '>'+sm_i18n.messages.PhotoSizeLarge+'<');
    }
    if (sm_i18n.messages.PhotoSizeXLarge) {
        html = html.replace(/>XLarge</g, '>'+sm_i18n.messages.PhotoSizeXLarge+'<');
    }
    if (sm_i18n.messages.PhotoSizeX2Large) {
        html = html.replace(/>X2Large</g, '>'+sm_i18n.messages.PhotoSizeX2Large+'<');
    }
    if (sm_i18n.messages.PhotoSizeOriginal) {
        html = html.replace(/>Original</g, '>'+sm_i18n.messages.PhotoSizeOriginal+'<');
    }
    return html;
};

sm_i18n.protectQuotes = function (str) {
    if (!str) {
        return str;
    }
    return str.replace(/\'/g, '\\\'').replace(/\"/g, '\\\"');
};

sm_i18n.objectToString = function(obj) {
    if (typeof(obj) == 'string') {
        return '\''+obj+'\'';
    } else if (typeof(obj) == 'object') {
        var str = '';
        for (var prop in obj) {
            if (obj.hasOwnProperty(prop)) {
                if (str.length > 0) {
                    str += '; ';
                }
                str += (sm_i18n.objectToString(prop)+': '+sm_i18n.objectToString(obj[prop]));
            }
        }
        return '['+str+']';
    } else {
        return ''+obj;
    }
};

if (sm_i18n.messages.SlideshowFullscreen) {
    YE.onAvailable('fullscreen',
        function() {
            this.value = sm_i18n.messages.SlideshowFullscreen;
        });
}

if (sm_i18n.messages.SlideshowPause) {
    YE.onContentReady('pause',
        function() {
            var btn = YD.getFirstChildBy(this, function(e) { return YD.hasClass(e, 'pause_button'); });
            if (btn) {
                btn.value = sm_i18n.messages.SlideshowPause;
            }
        });
}

if (sm_i18n.messages.SlideshowPlay) {
    YE.onContentReady('play',
        function() {
            var btn = YD.getFirstChildBy(this, function(e) { return YD.hasClass(e, 'play_button'); });
            if (btn) {
                btn.value = sm_i18n.messages.SlideshowPlay;
            }
        });
}

if (sm_i18n.messages.PhotoNavigationOf) {
    YE.onContentReady('showing',
        function() {
            var span = YD.getNextSiblingBy(this, function(e) { return YD.hasClass(e, 'foreground'); });
            if (span) {
                span.innerHTML = span.innerHTML.replace(/of/, sm_i18n.messages.PhotoNavigationOf);
            }
    });
}

YE.onContentReady('slideshow',
    function() {
        var toolbox = YD.getFirstChildBy(this, function(e) { return YD.hasClass(e, 'toolsbox'); });
        if (toolbox) {
            if (sm_i18n.messages.SlideshowCaption) {
                toolbox.innerHTML = toolbox.innerHTML.replace(/caption:/, sm_i18n.messages.SlideshowCaption);
            }
            if (sm_i18n.messages.SlideshowSpeed) {
                toolbox.innerHTML = toolbox.innerHTML.replace(/speed:/, sm_i18n.messages.SlideshowSpeed);
            }
            if (sm_i18n.messages.SlideshowSize) {
                toolbox.innerHTML = toolbox.innerHTML.replace(/size:/, sm_i18n.messages.SlideshowSize);
            }
            if (sm_i18n.messages.SlideshowThumbs) {
                toolbox.innerHTML = toolbox.innerHTML.replace(/thumbs:/, sm_i18n.messages.SlideshowThumbs);
            }
            var speed = YD.get('speed');
            var opt = null;
            if (speed) {
                if (sm_i18n.messages.SlideshowSpeedFast) {
                    opt = YD.getFirstChildBy(speed, function(e) { return e.value === 'fast'; });
                    if (opt) {
                        opt.innerHTML = sm_i18n.messages.SlideshowSpeedFast;
                    }
                }
                if (sm_i18n.messages.SlideshowSpeedFast) {
                    opt = YD.getFirstChildBy(speed, function(e) { return e.value === 'med'; });
                    if (opt) {
                        opt.innerHTML = sm_i18n.messages.SlideshowSpeedMed;
                    }
                }
                if (sm_i18n.messages.SlideshowSpeedFast) {
                    opt = YD.getFirstChildBy(speed, function(e) { return e.value === 'slow'; });
                    if (opt) {
                        opt.innerHTML = sm_i18n.messages.SlideshowSpeedSlow;
                    }
                }
            }
            var size = YD.get('Size');
            if (size) {
                if (sm_i18n.messages.SlideshowSizeSmall) {
                    opt = YD.getFirstChildBy(size, function(e) { return e.value === 'Small'; });
                    if (opt) {
                        opt.innerHTML = sm_i18n.messages.SlideshowSizeSmall;
                    }
                }
                if (sm_i18n.messages.SlideshowSizeMed) {
                    opt = YD.getFirstChildBy(size, function(e) { return e.value === 'Medium'; });
                    if (opt) {
                        opt.innerHTML = sm_i18n.messages.SlideshowSizeMed;
                    }
                }
                if (sm_i18n.messages.SlideshowSizeLarge) {
                    opt = YD.getFirstChildBy(size, function(e) { return e.value === 'Large'; });
                    if (opt) {
                        opt.innerHTML = sm_i18n.messages.SlideshowSizeLarge;
                    }
                }
            }
        }
});

// i18n
if (sm_i18n.messages.FeaturedGalleries) {
    YE.onAvailable('featuredTitle', function() {this.innerHTML = sm_i18n.messages.FeaturedGalleries;});
}
if (sm_i18n.messages.MostPopularPhotos) {
    YE.onAvailable('popularTitle', function() { this.innerHTML = sm_i18n.messages.MostPopularPhotos;});
}
if (sm_i18n.messages.Galleries) {
    YE.onAvailable('galleryTitle', function() { this.innerHTML = sm_i18n.messages.Galleries;});
}
if (sm_i18n.messages.GalleryCategories) {
    YE.onAvailable('categoryTitle', function() { this.innerHTML = sm_i18n.messages.GalleryCategories;});
}
//YE.onContentReady('breadcrumb', function() { var a=YD.getFirstChildBy(this, function(e) { return e.tagName=='A' && YD.hasClass(e, 'nav'); }); if (a) a.innerHTML='Lista albumw'; });
//YE.onContentReady('breadCrumbTrail', function() { var a=YD.getFirstChildBy(this, function(e) { return e.tagName=='A' && YD.hasClass(e, 'nav'); }); if (a) a.innerHTML='Lista albumw'; });

if (sm_i18n.messages.HideGalleryComments) {
    YE.onAvailable('showGalleryComments',
        function() {
            this.innerHTML = this.innerHTML.replace(/hide gallery comments/, sm_i18n.messages.HideGalleryComments);
        }
    );
}

if (sm_i18n.messages.Comments) {
    YE.onAvailable('comment_header', 
        function() {
            this.innerHTML = this.innerHTML.replace(/\b(gallery *)?comments\b/i, sm_i18n.messages.Comments);
        }
    );
}

if (sm_i18n.messages.ButtonNewComment) {
    YE.onAvailable('addcommentButton',
        function() {
            this.innerHTML = this.innerHTML.replace(/add comment/i, sm_i18n.messages.ButtonNewComment);
    });
}

YE.onContentReady('addAlbumComment', 
    function() { 
        var h3=YD.getFirstChildBy(this, function(e) { return e.tagName==='H3'; });
        if (h3 && sm_i18n.messages.NewComment) {
            h3.innerHTML = h3.innerHTML.replace(/new comment:/gi, sm_i18n.messages.NewComment);
        }
        if (h3 && sm_i18n.messages.NewGalleryComment) {
            h3.innerHTML = h3.innerHTML.replace(/new gallery comment:/gi, sm_i18n.messages.NewGalleryComment);
        }
        if (h3 && sm_i18n.messages.CommentRequiresApproval) {
            h3.innerHTML = h3.innerHTML.replace(/requires approval/i, sm_i18n.messages.CommentRequiresApproval);
        }
    }
);

YE.onContentReady('galleryCommentRating', 
    function() {
        if (sm_i18n.messages.CommentOn) {
            this.innerHTML = this.innerHTML.replace(/comment on:/i, sm_i18n.messages.CommentOn);
        }
        if (sm_i18n.messages.CommentRating) {
            this.innerHTML = this.innerHTML.replace(/rating:/i, sm_i18n.messages.CommentRating);
        }
        if (sm_i18n.messages.CommentStars) {
            this.innerHTML = this.innerHTML.replace(/stars/i, sm_i18n.messages.CommentStars);
            var rating = YD.get('albumCommentRating');
            if (rating) {
                if (rating.nextSibling && rating.nextSibling.nodeType === 3) {
                    rating.nextSibling.nodeValue = rating.nextSibling.nodeValue.replace(/stars/i, sm_i18n.messages.CommentStars);
                }
            }
        }
    }
);

YE.onContentReady('commentType', 
    function() { 
        if (sm_i18n.messages.CommentTypeChoose) {
            var opt1=YD.getFirstChildBy(this, function(e) { return e.tagName==='OPTION' && e.value==='choose a type'; });
            if (opt1) {
                opt1.innerHTML = sm_i18n.messages.CommentTypeChoose;
            }
        }
        if (sm_i18n.messages.CommentTypeImage) {
            var opt2=YD.getFirstChildBy(this, function(e) { return e.tagName==='OPTION' && e.value==='Image'; });
            if (opt2) {
                opt2.innerHTML = sm_i18n.messages.CommentTypeImage;
            }
        }
        if (sm_i18n.messages.CommentTypeGallery) {
            var opt3=YD.getFirstChildBy(this, function(e) { return e.tagName==='OPTION' && e.value==='Album'; });
            if (opt3) {
                opt3.innerHTML = sm_i18n.messages.CommentTypeGallery;
            }
        }
    }
);


YE.onContentReady('albumCommentRating', 
    function() {
        var opt = null;
        if (sm_i18n.messages.CommentRatingNone) {
            opt=YD.getFirstChildBy(this, function(e) { return e.tagName==='OPTION' && e.value==='0'; });
            if (opt) {
                opt.innerHTML = sm_i18n.messages.CommentRatingNone;
            }
        }
        if (sm_i18n.messages.CommentRating1Star) {
            opt=YD.getFirstChildBy(this, function(e) { return e.tagName==='OPTION' && e.value==='1'; });
            if (opt) {
                opt.innerHTML = sm_i18n.messages.CommentRating1Star;
            }
        }
        if (sm_i18n.messages.CommentRating2Stars) {
            opt=YD.getFirstChildBy(this, function(e) { return e.tagName==='OPTION' && e.value==='2'; });
            if (opt) {
                opt.innerHTML = sm_i18n.messages.CommentRating2Stars;
            }
        }
        if (sm_i18n.messages.CommentRating3Stars) {
            opt=YD.getFirstChildBy(this, function(e) { return e.tagName==='OPTION' && e.value==='3'; });
            if (opt) {
                opt.innerHTML = sm_i18n.messages.CommentRating3Stars;
            }
        }
        if (sm_i18n.messages.CommentRating4Stars) {
            opt=YD.getFirstChildBy(this, function(e) { return e.tagName==='OPTION' && e.value==='4'; });
            if (opt) {
                opt.innerHTML = sm_i18n.messages.CommentRating4Stars;
            }
        }
        if (sm_i18n.messages.CommentRating5Stars) {
            opt=YD.getFirstChildBy(this, function(e) { return e.tagName==='OPTION' && e.value==='5'; });
            if (opt) {
                opt.innerHTML = sm_i18n.messages.CommentRating5Stars;
            }
        }
    }
);

YE.onContentReady('addAlbumComment',
    function() {
        if (sm_i18n.messages.ButtonAddComment) {
            var btnAdd = YD.getFirstChildBy(this, function(e) { return e.tagName === 'INPUT' && e.value.toLowerCase() === 'add comment'; });
            if (btnAdd) {
                btnAdd.value = sm_i18n.messages.ButtonAddComment;
            }
        }
        if (sm_i18n.messages.ButtonCancelComment) {
            var btnCancel = YD.getFirstChildBy(this, function(e) { return e.tagName === 'INPUT' && e.value.toLowerCase() === 'cancel'; });
            if (btnCancel) {
                btnCancel.value = sm_i18n.messages.ButtonCancelComment;
            }
        }
    }
);

if (sm_i18n.messages.CommentName) {
    YE.onAvailable('galleryCommentName', 
        function() { 
            this.innerHTML = this.innerHTML.replace(/name:/i, sm_i18n.messages.CommentName);
        }
    );
}

if (sm_i18n.messages.CommentLink) {
    YE.onAvailable('galleryCommentLink', 
        function() { 
            this.innerHTML = this.innerHTML.replace(/link:/i, sm_i18n.messages.CommentLink);
        }
    );
}

YE.onContentReady('galleryCommentCaptcha', 
    function() { 
        if (sm_i18n.messages.CommentCaptchaFoilSpammers) {
            this.innerHTML = this.innerHTML.replace(/to foil spammers, enter this code:/gi, sm_i18n.messages.CommentCaptchaFoilSpammers);
        }
        if (sm_i18n.messages.CommentCaptchaInThisBox) {
            this.innerHTML = this.innerHTML.replace(/in this box:/gi, sm_i18n.messages.CommentCaptchaInThisBox);
        }
        if (sm_i18n.messages.CommentCaptchaCodeUnreadable) {
            this.innerHTML = this.innerHTML.replace(/Code unreadable/gi, sm_i18n.messages.CommentCaptchaCodeUnreadable);
        }
    }
);

sm_i18n.albumNav = function() {
    if (sm_i18n.messages.GalleryPages) {
        var pageNav = YD.getFirstChildBy(this, function(e) { return YD.hasClass(e, 'pageNav'); });
        if (pageNav) {
            var title = YD.getFirstChildBy(pageNav, function(e) { return YD.hasClass(e, 'title'); });
            if (title) {
                title.innerHTML = title.innerHTML.replace(/Gallery pages:/, sm_i18n.messages.GalleryPages);
            }
        }
    }
    var photoNav = YD.getFirstChildBy(this, function(e) { return YD.hasClass(e, 'photoNav'); });
    if (photoNav) {
        if (sm_i18n.messages.GalleryNavigationNext) {
            photoNav.innerHTML = photoNav.innerHTML.replace(/\bNext\b/, sm_i18n.messages.GalleryNavigationNext);
        }
        if (sm_i18n.messages.GalleryNavigationPrev) {
            photoNav.innerHTML = photoNav.innerHTML.replace(/\bPrev\b/, sm_i18n.messages.GalleryNavigationPrev);
        }
        if (sm_i18n.messages.GalleryNavigationOf) {
            photoNav.innerHTML = photoNav.innerHTML.replace(/\bof\b/, sm_i18n.messages.GalleryNavigationOf);
        }
    }
};

YE.onContentReady('albumNav_bottom', sm_i18n.albumNav);
YE.onContentReady('albumNav_top', sm_i18n.albumNav);

YE.onContentReady('sizePicker',
  function() {
      this.innerHTML = sm_i18n.photoSizes(this.innerHTML);
      if (sm_i18n.messages.PhotoViewingSize) {
          this.innerHTML = this.innerHTML.replace(/photo viewing size:/i, sm_i18n.messages.PhotoViewingSize);
          this.innerHTML = this.innerHTML.replace(/sizes:/i, sm_i18n.messages.PhotoViewingSize);
      }
      if (sm_i18n.messages.PhotoViewingPreferredSize) {
          this.innerHTML = this.innerHTML.replace(/your preferred size:/i, sm_i18n.messages.PhotoViewingPreferredSize);
      }
});

YE.onContentReady('lightBoxNav',
  function() {
      if (sm_i18n.messages.PhotoSizes) {
          this.innerHTML = this.innerHTML.replace(/sizes:/i, sm_i18n.messages.PhotoSizes);
      }
      this.innerHTML = sm_i18n.photoSizes(this.innerHTML);
});

YE.onContentReady('stylebar',
    function() {
        function style(str) {
            if (sm_i18n.messages.StyleSmugMug && str.toLowerCase().indexOf('smugmug') >= 0) {
                str = str.replace(/smugmug/i, sm_i18n.messages.StyleSmugMug);
            }
            if (sm_i18n.messages.StyleTraditional && str.toLowerCase().indexOf('traditional') >= 0) {
                str = str.replace(/traditional/i, sm_i18n.messages.StyleTraditional);
            }
            if (sm_i18n.messages.StyleAllThumbs && str.toLowerCase().indexOf('all thumbs') >= 0) {
                str = str.replace(/all thumbs/i, sm_i18n.messages.StyleAllThumbs);
            }
            if (sm_i18n.messages.StyleSlideshow && str.toLowerCase().indexOf('slideshow') >= 0) {
                str = str.replace(/slideshow/i, sm_i18n.messages.StyleSlideshow);
            }
            if (sm_i18n.messages.StyleJournal && str.toLowerCase().indexOf('journal') >= 0) {
                str = str.replace(/journal/i, sm_i18n.messages.StyleJournal);
            }
            if (sm_i18n.messages.StyleFilmstrip && str.toLowerCase().indexOf('filmstrip') >= 0) {
                str = str.replace(/filmstrip/i, sm_i18n.messages.StyleFilmstrip);
            }
            if (sm_i18n.messages.StyleCritique && str.toLowerCase().indexOf('critique') >= 0) {
                str = str.replace(/critique/i, sm_i18n.messages.StyleCritique);
            }
            return str;
        }
        if (sm_i18n.messages.Style) {
            var span = YD.getFirstChildBy(this, function(e) { return YD.hasClass(e, 'title'); });
            if (span) {
                span.innerHTML = span.innerHTML.replace(/style:/i, sm_i18n.messages.Style);
            }
        }
        var form = YD.getFirstChildBy(this, function(e) { return e.name==='template_chooser'; });
        if (form) {
            var sel = YD.getFirstChildBy(form, function(e) { return e.name==='TemplateID'; });
            if (sel) {
                var opts = YD.getChildrenBy(sel, function(e) { return e.tagName==='OPTION'; });
                for (var i=0; i < opts.length; i++) {
                    opts[i].innerHTML = style(opts[i].innerHTML);
                }
            }
        }
});

(function() {
    function _First(array) {
        if (array.length && array.length > 0) {
            return array[0];
        }
        return null;
    }

    YE.onContentReady('content',
        function() {
            if (YD.hasClass(content, 'passwordPage')) {
                var title = YD.getFirstChildBy(this, function(e) { return e.tagName === 'H2' && YD.hasClass(e, 'title'); });
                if (sm_i18n.messages.GalleryRequiresPassword && title) {
                    title.innerHTML = sm_i18n.messages.GalleryRequiresPassword;
                }
                var alert = YD.getFirstChildBy(this, function(e) { return e.tagName === 'H2' && YD.hasClass(e, 'alert'); });
                if (sm_i18n.messages.GalleryWrongPassword && alert) {
                    alert.innerHTML = sm_i18n.messages.GalleryWrongPassword;
                }
                var formInput = _First(YD.getElementsByClassName('formInput', 'DIV', this));
                if (formInput) {
                    var password = YD.getFirstChildBy(formInput, function(e) { return e.tagName === 'DIV' && YD.hasClass(e, 'title') && /password:/i.test(e.innerHTML); });
                    if (sm_i18n.messages.GalleryPassword && password) {
                        password.innerHTML = sm_i18n.messages.GalleryPassword;
                    }
                }
                var form = YD.getFirstChildBy(this, function(e) { return e.tagName === 'FORM'; });
                var cookie = YD.getFirstChildBy(form, function(e) { return e.tagName === 'P' && /i'm using a public computer, don't save this password./i.test(e.innerHTML); });
                if (cookie && sm_i18n.messages.GalleryPasswordPublicComputer) {
                    cookie.innerHTML = cookie.innerHTML.replace(/i'm using a public computer, don't save this password./i, sm_i18n.messages.GalleryPasswordPublicComputer);
                }
                var formButtons = YD.getFirstChildBy(form, function(e) { return YD.hasClass(e, 'formButtons'); });
                var submit = YD.getFirstChildBy(formButtons, function(e) { return e.tagName === 'INPUT' && YD.hasClass(e, 'submit_button'); });
                if (submit && sm_i18n.messages.GalleryPasswordSubmit) {
                    submit.value = sm_i18n.messages.GalleryPasswordSubmit;
                }
                if (YD.inDocument('hideCookies')) {
                    var hide = YD.get('hideCookies');
                    if (hide && sm_i18n.messages.GalleryPasswordEnableCookies) {
                        hide.innerHTML = hide.innerHTML.replace(/Please enable cookies for this page./i, sm_i18n.messages.GalleryPasswordEnableCookies);
                    }
                    if (hide && sm_i18n.messages.GalleryPasswordHeresHow) {
                        hide.innerHTML = hide.innerHTML.replace(/here's how/i, sm_i18n.messages.GalleryPasswordHeresHow);
                    }
                }
            }
    });
})();
